package zxs.sino.elite.hub.enums;


import java.util.Objects;

/**
 * 通用枚举接口
 * @author zhang
 */
public interface BaseEnum<T> {

    /**
     * 获取枚举的key
     */
    T getCode();

    /**
     * 获取枚举的值
     */
    String getValue();

    static <T, E extends Enum<E> & BaseEnum<T>> E getByCode(Class<E> enumClass, T code) {
        for (E enumConstant : enumClass.getEnumConstants()) {
            if (enumConstant.getCode().equals(code)) {
                return enumConstant;
            }
        }
        return null;
    }

    static <T, E extends Enum<E> & BaseEnum<T>> String getValueByCode(Class<E> enumClass, T code) {
        return Objects.requireNonNull(getByCode(enumClass, code)).getValue();
    }

    static <T, E extends Enum<E> & BaseEnum<T>> boolean isType(Class<E> enumClass, T code, E targetType) {
        E match = getByCode(enumClass, code);
        return match == targetType;
    }
}
